import { login, logout, getInfo } from '@/api/login'
import { getToken, setToken, removeToken, getbaseInfo, setbaseInfo, removebaseInfo } from '@/utils/auth'


const user = {
  state: {
    token: getToken(),
    baseInfo:getbaseInfo()?JSON.parse(getbaseInfo()):'',
    name: '',
    avatar: 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1585297970246&di=ac4eea38fb7f45681f5ddff6c14156e0&imgtype=0&src=http%3A%2F%2Fwww.icosky.com%2Ficon%2Fpng%2FSystem%2FScrap%2FAdministrator%25203.png',
    roles: []
  },

  mutations: {
    // 储存token
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    // 储存用户名
    SET_NAME: (state, name) => {
      state.name = name
    },
    // 储存头像
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    // 储存路由表
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    // 储存用户信息
    SET_INFO: (state,info) => {
      state.baseInfo = info
    }
  },

  actions: {
    // 登录
    Login({commit }, userInfo) {
      // trim去除头尾空格字符串
      const username = userInfo.username.trim()

      return new Promise((resolve, reject) => {
        // return
        login(username, userInfo.password).then(response => {
        
          const data = response;
          console.log(data);
          const tokenStr = data.obj.token
          const baseInfo=data.obj;
          setToken(tokenStr);
          setbaseInfo(baseInfo);
          // 存token vuex
          commit('SET_TOKEN', tokenStr)
          // 存信息 vuex
          commit('SET_INFO', baseInfo)
          resolve()
        }).catch(error => {
          console.log(error);
          reject(error)
        })
      })
    },
    // 代理商获取信息
    // GetInfo({commit}){
    //   return new Promise((resolve, reject)=>{
    //     getInfo().then(response=>{
    //       const data = response;
    //       const baseInfo=data.obj;
    //       console.log(data);
    //       setbaseInfo(baseInfo);
    //       commit('SET_INFO', baseInfo)
    //       resolve()
    //     }).catch(error =>{
    //       console.log(error);
    //       reject(error)
    //     })
    //   })
    // },
    // 获取用户信息
    GetInfo({ commit, state }) {
      return new Promise((resolve, reject) => {
        getInfo().then(response => {
          const data = response.data
          if (data.roles && data.roles.length > 0) { // 验证返回的roles是否是一个非空数组
            commit('SET_ROLES', data.roles)
          } else {
            reject('getInfo: roles must be a non-null array !')
          }
          commit('SET_NAME', data.username)
          commit('SET_AVATAR', data.icon)
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 登出
    LogOut({ commit, state }) {
      return new Promise((resolve, reject) => {
        logout(state.token).then(() => {
          commit('SET_TOKEN', '')
          commit('SET_ROLES', [])
          removeToken()
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 前端 登出
    FedLogOut({ commit }) {
      return new Promise(resolve => {
        commit('SET_TOKEN', '')
        commit('SET_INFO', '')
        removebaseInfo()
        removeToken()
        resolve()
      })
    }
  }
}

export default user
